<?php
/**
 * 活动的控制器类
 */ 
class ActivityAction extends HomeAction{
	/**
	 * 添加活动
	 */
	public function add(){
		if($_POST){
			$_POST['thumb']			= $_POST['images'][0];
			$_POST['scenic_images']	= json_encode($_POST['images']);
			$Activity = D('Activity');
			$_POST['uid'] = Session::get('uid');
			if($Activity->create()){
				$Activity->add();
				$this->success("活动添加成功");
			}else{
				$this->error($Activity->geterror());
			}
		}else{
			$this->display();
		}
	}
	public function update(){
		$id=$_REQUEST['id'];
		$Activity = D('Activity');
		/**
		 * 判断这个活动是否可以修改，如果已经审核，有人报名(除创建者之外)，或者活动过期将不可修改。
		 */
		$activity_info = $Activity->where("id={$id}")->find();
		if($_SERVER['REQUEST_TIME'] <$activity_info['start_time']){
		}
		
		if($_POST){
			if($Activity->create()){
				$Activity->save();
				$this->success("修改成功");
			}else{
				$this->error($Activity->geterror());
			}
		}else{
			$this->assign('activity_info',$activity_info);
			$this->display();
		}
	}
	/**
	 * 近期活动列表
	 */
	public function index(){
		$Activity = M('Activity');
		$field = "activity.*,user.username,user.sex";
		$where = "1 AND activity.state=1";
		if($_GET['keyword']){
			$where .= " AND activity.name LIKE '%{$_GET['keyword']}%'";
		}
		$order = "activity.create_time DESC";
		$limit = C('PAGE_COUNT');
		$count = $Activity->where($where)->count();
		$Page = new Page($count, C('PAGE_COUNT'));
		$limit = "{$Page->firstRow},{$Page->listRows}";
		$join = "LEFT JOIN `user` ON user.uid=activity.uid";
		$list = $Activity->where($where)->limit($limit)->join($join)->field($field)->order($order)->select();
		$Participation = M('participation');
		foreach($list as &$v){
			$tmp = json_decode($v['scenic_images']);
			$v['thumb'] = $tmp[0];
			$v['images']=array_slice($tmp,0,3);
			$where="uid={$_SESSION['uid']} AND acid={$v['id']}";
			$count = $Participation->where($where)->count();
			if($count){	$v['if_reg']=1;	}else{$v['if_reg']=0;}
			/**
			 * 获取已经报名人员男女比例
			 */
			$field = "count(*) as `count`,user.sex";
			$where="acid = {$v['id']}";
			$group="user.sex";
			$join = "LEFT JOIN `user` ON user.uid=participation.uid";
			$proportion = (array)$Participation->where($where)->field($field)->join($join)->group($group)->select();
			$v['reg_man']=$v['reg_woman']=0;
			foreach($proportion as $v1){
				switch($v1['sex']){
					case '1':
						$v['reg_woman']=$v1['count'];
						break;
					case '0':
						$v['reg_man']=$v1['count'];
						break;
				}
			}
		}
		$this->assign('page',$Page->show());
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 活动详情
	 */
	public function view(){
		$id = $_GET['id'];
		$Activity = M('Activity');
		$activity_info = $Activity->where("id={$id}")->find();
		$this->assign('activity_info',$activity_info);
		$this->display();
	}
	/**
	 *报名活动 
	 */
	public function register(){
		$aid = intval($_GET['id']);
		$Participation = M('participation');
		$data['acid'] = $aid;
		$data['uid'] = Session::get('uid');
		$data['register_time']=$_SERVER['REQUEST_TIME'];
		$Participation->add($data);
		$this->success('报名成功');
	}
}
?>